package com.woniu.repaircenter.service.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.repaircenter.models.dto.RepairCategoryDTO;
import com.woniu.repaircenter.models.form.SelectRepairCategoryForm;
import com.woniu.repaircenter.models.form.UpdateRepairCategoryForm;
import com.woniu.repaircenter.models.param.SelectRepairCategoryParam;
import com.woniu.repaircenter.models.param.UpdateRepairCategoryParam;
import com.woniu.repaircenter.service.service.RepairCategoryService;
import com.woniu.utils.JwtUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修类别表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/repairCategory")
@Api(tags = "维修类别管理")
public class RepairCategoryController {

    @Autowired
    private RepairCategoryService repairCategoryService;

    /**
     * 新增维修类型
     * @param token        获取用户名称
     * @return 返回结果
     * @throws Exception 异常
     */
    @PostMapping("/add")
    @ApiOperation("新增维修类型接口")
    @RequireRole("管理员")
    @RequirePermission("新增维修类型")
    public Result addRepairCategory(
            @ApiParam(name = "categoryName", value = "类型名称", required = true) String categoryName,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        Integer result = repairCategoryService.addRepairCategory(categoryName, adminName);
        if (result == 1) {
            return Result.getResult().setMsg("类型添加成功").setCode(200);
        } else {
            return Result.getResult().setMsg("类型添加失败").setCode(500);
        }
    }

    /**
     * 获取维修类型列表
     * @param srcf 查询条件
     * @return 返回结果
     * @throws Exception 异常
     */
    @PostMapping("/list")
    @ApiOperation("获取维修类型列表接口")
    public Result listRepairCategory(
            @RequestBody @ApiParam(name = "srcf", value = "查询条件", required = true) SelectRepairCategoryForm srcf) throws Exception {
        PageInfo<RepairCategoryDTO> list = repairCategoryService.listRepairCategory(BeanUtil.toBean(srcf, SelectRepairCategoryParam.class));
        return Result.getResult(PageInfo.class).setData(list).setCode(200).setMsg("获取维修类型列表成功");
    }

    /**
     * 获取所有维修类型列表
     * @return 返回结果
     * @throws Exception 异常
     */
    @GetMapping("/listAll")
    @ApiOperation("获取所有维修类型列表接口")
    public Result listAllRepairCategory() throws Exception {
        List<RepairCategoryDTO> list = repairCategoryService.listAllRepairCategory();
        return Result.getResult(List.class).setData(list).setCode(200).setMsg("获取所有维修类型列表成功");
    }


    /**
     * 禁用维修类型
     * @param repairCategoryId 维修类型ID
     * @param token             获取用户名称
     * @return 返回结果
     * @throws Exception 异常
     */
    @PutMapping("/forbiddenStatus")
    @ApiOperation("禁用维修类型接口")
    @RequireRole("管理员")
    @RequirePermission("禁用维修类型")
    public Result updateStatus(
            @ApiParam(name = "repairCategoryId", value = "维修类型ID", required = true)@RequestParam("repairCategoryId") Integer repairCategoryId,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        Integer result = repairCategoryService.updateStatus(repairCategoryId, adminName);
        if (result == 1) {
            return Result.getResult().setMsg("修改成功").setCode(200);
        } else {
            return Result.getResult().setMsg("修改失败").setCode(500);
        }
    }

    /**
     *
     * @param repairCategoryId
     * @param token
     * @return
     * @throws Exception
     */
   @PutMapping("/enableStatus")
   @ApiOperation("启用维修类型接口")
   @RequireRole("管理员")
   @RequirePermission("启用维修类型")
   public Result updateSort(
           @ApiParam(name = "repairCategoryId", value = "维修类型ID", required = true)@RequestParam("repairCategoryId") Integer repairCategoryId,
           @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
       String adminName = JwtUtil.parseToken(token).get("adminName").toString();
       Integer result = repairCategoryService.updateStatus(repairCategoryId,  adminName);
       if (result == 1) {
           return Result.getResult().setMsg("修改成功").setCode(200);
       } else {
           return Result.getResult().setMsg("修改失败").setCode(500);
       }
   }

    /**
     * 修改维修类型
     * @param urcf  修改参数
     * @param token 获取用户名称
     * @return 返回结果
     * @throws Exception 异常
     */
    @PutMapping("/updateCategory")
    @ApiOperation(value = "修改维修类别", notes = "修改维修类别")
    @RequireRole("管理员")
    @RequirePermission("修改维修类别")
    public Result updateCategory(
            @Valid @RequestBody @ApiParam(name = "urcf", value = "修改参数", required = true) UpdateRepairCategoryForm urcf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        Integer result = repairCategoryService.updateCategory(BeanUtil.toBean(urcf, UpdateRepairCategoryParam.class), adminName);
        if (result == 1) {
            return Result.getResult().setMsg("修改成功").setCode(200);
        } else {
            return Result.getResult().setMsg("修改失败").setCode(500);
        }
    }
}
